package com.lb.jwitter.action;


 import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
 import java.util.Collection;
import java.util.Date;
import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider; 
import com.lb.jwitter.util.ThoiKhoaBieuPhongNgay;
import com.lb.jwitter.util.ThoiKhoaBieuPhongNgayMonHoc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 import com.opensymphony.xwork2.Action; 
 import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
 import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThuc;
 import com.lb.jwitter.service.TblQLDTTXAThoiKhoaBieuChinhThucService;
 import com.lb.jwitter.service.TblQLDTTXADMNgayTrongTuan;
 import com.lb.jwitter.service.TblQLDTTXADMNgayTrongTuanService;

import com.lb.jwitter.service.TblQLDTTXADMPhong;
 import com.lb.jwitter.service.TblQLDTTXADMPhongService;

import com.lb.jwitter.service.TblQLDTTXADMTietHoc;
 import com.lb.jwitter.service.TblQLDTTXADMTietHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
 import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAThoiKhoaBieuChinhThucHocVienSearchAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }
    
    private int view;

    private Collection<TblQLDTTXAThoiKhoaBieuChinhThuc> tblQLDTTXAThoiKhoaBieuChinhThucs; 
    public Collection<TblQLDTTXAThoiKhoaBieuChinhThuc>  getTblQLDTTXAThoiKhoaBieuChinhThucs() {
         return tblQLDTTXAThoiKhoaBieuChinhThucs; 
      } 

    private TblQLDTTXAThoiKhoaBieuChinhThucService tblQLDTTXAThoiKhoaBieuChinhThucService; 

    private List<TblQLDTTXADMNgayTrongTuan> tblQLDTTXADMNgayTrongTuans; 
    public List<TblQLDTTXADMNgayTrongTuan>  getTblQLDTTXADMNgayTrongTuans(){ 
              return tblQLDTTXADMNgayTrongTuans; 
     } 

    private TblQLDTTXADMNgayTrongTuanService tblQLDTTXADMNgayTrongTuanService; 

    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 
    
    private TblQLDTTXAHocVien tblQLDTTXAHocVien;
    private String mSHV;

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private List<TblQLDTTXADMTietHoc> tblQLDTTXADMTietHocs; 
    public List<TblQLDTTXADMTietHoc>  getTblQLDTTXADMTietHocs(){ 
              return tblQLDTTXADMTietHocs; 
     } 

    private List<ThoiKhoaBieuPhongNgay> thoiKhoaBieuPhongNgays;
    private TblQLDTTXADMTietHocService tblQLDTTXADMTietHocService; 

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs; 
    public List<TblQLDTTXALopMonHoc>  getTblQLDTTXALopMonHocs(){ 
              return tblQLDTTXALopMonHocs; 
     } 
    
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;
    
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;
    private List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens;
    
    private Long hocKyId;

    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService; 

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_ThoiKhoaBieuChinhThuc", "xem")){ 
        return "noprivilege"; 
    }
    tblQLDTTXAThoiKhoaBieuChinhThucService = (TblQLDTTXAThoiKhoaBieuChinhThucService) context.getBean("tbl_QLDTTXA_ThoiKhoaBieuChinhThucClient"); 
         tblQLDTTXADMNgayTrongTuanService = (TblQLDTTXADMNgayTrongTuanService) context.getBean("tbl_QLDTTXA_DM_NgayTrongTuanClient"); 

        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXADMTietHocService = (TblQLDTTXADMTietHocService) context.getBean("tbl_QLDTTXA_DM_TietHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient"); 
        
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient"); 

        
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        
        //get hocky
        Date today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1(hocKyId);
            con.setOperator("eq");
            cons.add(con);	
        } else{
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            con.setType("date");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");
            con2.setType("date");
            cons.add(con2);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
            tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        }

        //Get hocVien
        cons.clear();
        if(mSHV != null && !mSHV.equalsIgnoreCase("")){
            ConditionReport con = new ConditionReport(); 
            con.setCol("MSHV");
            con.setValue1(mSHV);
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("MSHV");
            con.setValue1("");
            con.setOperator("eq");
            cons.add(con);	
        }
        
        tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
        if(tblQLDTTXAHocViens != null && tblQLDTTXAHocViens.size() > 0)
        	tblQLDTTXAHocVien = tblQLDTTXAHocViens.get(0);
        
        //LopHoc cua HocVien
        cons.clear();
        if(tblQLDTTXAHocVien != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(tblQLDTTXAHocVien.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(new Long(0));
            con.setOperator("eq");
            cons.add(con);	
        }
        if(tblQLDTTXAHocKyTrongNam != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("lopMonHocId.hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("lopMonHocId.hocKyId.id");
            con.setValue1("");
            con.setOperator("eq");
            cons.add(con);	
        }
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("trangThai"); 
            con.setValue1(2); 
            con.setOperator("ne"); 
            cons.add(con); 
        }
        tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
        
        
        cons.clear();
        //hocKy
        if(tblQLDTTXAHocKyTrongNam != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1("");
            con.setOperator("max");
            cons.add(con);	
        }
        //order
        {
        	ConditionReport con = new ConditionReport(); 
            con.setCol("ngayId.id");
            con.setValue1("asc");
            con.setOperator("order");
            cons.add(con);
        }
        {
        	ConditionReport con = new ConditionReport(); 
            con.setCol("tietHocId.id");
            con.setValue1("asc");
            con.setOperator("order");
            cons.add(con);
        }
        
      List<TblQLDTTXAThoiKhoaBieuChinhThuc> tkbChinhThucTemps = new ArrayList<TblQLDTTXAThoiKhoaBieuChinhThuc>();
      
      tblQLDTTXAThoiKhoaBieuChinhThucs = tblQLDTTXAThoiKhoaBieuChinhThucService.findtblQLDTTXAThoiKhoaBieuChinhThucByCondition(cons,0, 0); 
      for(TblQLDTTXAThoiKhoaBieuChinhThuc tkbTemp : tblQLDTTXAThoiKhoaBieuChinhThucs){
    	boolean b= false;
    	for(TblQLDTTXALopMonHocHocVien lopMonHocHocVien : tblQLDTTXALopMonHocHocViens){
    		if(lopMonHocHocVien.getLopMonHocId().getId().equals(tkbTemp.getLopMonHocId().getId())){
    			b= true;
    			break;
    		}
    	}
    	
    	if(b) tkbChinhThucTemps.add(tkbTemp);
    	  
      }
      tblQLDTTXAThoiKhoaBieuChinhThucs = tkbChinhThucTemps;
      
      tblQLDTTXADMNgayTrongTuans = tblQLDTTXADMNgayTrongTuanService.findAlltblQLDTTXADMNgayTrongTuan(0, 0);

      tblQLDTTXADMTietHocs = tblQLDTTXADMTietHocService.findAlltblQLDTTXADMTietHoc(0, 0);
      
      cons.clear();
      {
          ConditionReport con = new ConditionReport(); 
          con.setCol("namHocId.id");
          con.setValue1("desc");
          con.setOperator("order");
          cons.add(con);	  
      }
      {
          ConditionReport con = new ConditionReport(); 
          con.setCol("thoiGianBatDau");
          con.setValue1("desc");
          con.setOperator("order");
          cons.add(con);	  
      }
      tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 20, 1);

      //tao thoi khoa bieu
      thoiKhoaBieuPhongNgays  = new ArrayList<ThoiKhoaBieuPhongNgay>();
      
      for(TblQLDTTXADMNgayTrongTuan ngayTrongTuan : tblQLDTTXADMNgayTrongTuans){
      	ThoiKhoaBieuPhongNgay thoiKhoaBieuPhongNgay = new ThoiKhoaBieuPhongNgay();

      	thoiKhoaBieuPhongNgay.setNgay(ngayTrongTuan);
      	
      	thoiKhoaBieuPhongNgay
      		.setThoiKhoaBieuPhongNgayMonHocs
      			(new ArrayList<ThoiKhoaBieuPhongNgayMonHoc>());
      	
      	//thoi khoa bieu cho ngay thu i
      	List<TblQLDTTXAThoiKhoaBieuChinhThuc> tkbNgays = new ArrayList<TblQLDTTXAThoiKhoaBieuChinhThuc>();
      	for(TblQLDTTXAThoiKhoaBieuChinhThuc thoiKhoaBieuChinhThuc : tblQLDTTXAThoiKhoaBieuChinhThucs){
	        	if(thoiKhoaBieuChinhThuc.getNgayId().getId().equals(thoiKhoaBieuPhongNgay.getNgay().getId()))
	        	{
	        		tkbNgays.add(thoiKhoaBieuChinhThuc);
	        	}
      	}
      	
      	Long lopId_old = new Long(0);
      	int lopId_new = -1;

      	
		ThoiKhoaBieuPhongNgayMonHoc thoiKhoaBieuPhongNgayMonHoc = new ThoiKhoaBieuPhongNgayMonHoc();

      	for(TblQLDTTXAThoiKhoaBieuChinhThuc tkbNgay : tkbNgays){
      		if(!tkbNgay.getLopMonHocId().getId().equals(lopId_old) ||
          			tkbNgay.getTietHocId().getId().compareTo(thoiKhoaBieuPhongNgayMonHoc.getTietBatDau().getId() + thoiKhoaBieuPhongNgayMonHoc.getSoTiet()) == 1 ||
          			tkbNgay.getTietHocId().getId().compareTo(thoiKhoaBieuPhongNgayMonHoc.getTietBatDau().getId() + thoiKhoaBieuPhongNgayMonHoc.getSoTiet()) == -1 ){
          			
      			thoiKhoaBieuPhongNgay.getThoiKhoaBieuPhongNgayMonHocs().add(thoiKhoaBieuPhongNgayMonHoc);
          			
      			thoiKhoaBieuPhongNgayMonHoc = new ThoiKhoaBieuPhongNgayMonHoc<TblQLDTTXAThoiKhoaBieuChinhThuc, TblQLDTTXALopMonHoc>();

			    thoiKhoaBieuPhongNgayMonHoc.setThoiKhoaBieu(tkbNgay);
			    thoiKhoaBieuPhongNgayMonHoc.setLopMonHoc(tkbNgay.getLopMonHocId());
			    thoiKhoaBieuPhongNgayMonHoc.setTietBatDau(tkbNgay.getTietHocId());
			    thoiKhoaBieuPhongNgayMonHoc.setSoTiet(1);
			    
      		}else {
      			thoiKhoaBieuPhongNgayMonHoc.setSoTiet(thoiKhoaBieuPhongNgayMonHoc.getSoTiet() + 1);	
      		}
      		lopId_old = tkbNgay.getLopMonHocId().getId();
      	}
      	thoiKhoaBieuPhongNgay.getThoiKhoaBieuPhongNgayMonHocs().add(thoiKhoaBieuPhongNgayMonHoc);
		
      	thoiKhoaBieuPhongNgays.add(thoiKhoaBieuPhongNgay);
      }//end for ngay
      
      
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_QLDTTXA_ThoiKhoaBieuChinhThuc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
}

public void setTblQLDTTXAHocKyTrongNam(
		TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
	this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
}

public Long getHocKyId() {
	return hocKyId;
}

public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
}



public List<ThoiKhoaBieuPhongNgay> getThoiKhoaBieuPhongNgays() {
	return thoiKhoaBieuPhongNgays;
}

public void setThoiKhoaBieuPhongNgays(
		List<ThoiKhoaBieuPhongNgay> thoiKhoaBieuPhongNgays) {
	this.thoiKhoaBieuPhongNgays = thoiKhoaBieuPhongNgays;
}


public int getView() {
	return view;
}

public TblQLDTTXAHocVien getTblQLDTTXAHocVien() {
	return tblQLDTTXAHocVien;
}

public void setTblQLDTTXAHocVien(TblQLDTTXAHocVien tblQLDTTXAHocVien) {
	this.tblQLDTTXAHocVien = tblQLDTTXAHocVien;
}

public String getMSHV() {
	return mSHV;
}

public void setMSHV(String mshv) {
	mSHV = mshv;
}

public List<TblQLDTTXALopMonHocHocVien> getTblQLDTTXALopMonHocHocViens() {
	return tblQLDTTXALopMonHocHocViens;
}

public void setTblQLDTTXALopMonHocHocViens(
		List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens) {
	this.tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocViens;
}

public void setTblQLDTTXAThoiKhoaBieuChinhThucs(
		Collection<TblQLDTTXAThoiKhoaBieuChinhThuc> tblQLDTTXAThoiKhoaBieuChinhThucs) {
	this.tblQLDTTXAThoiKhoaBieuChinhThucs = tblQLDTTXAThoiKhoaBieuChinhThucs;
}

public void setTblQLDTTXADMNgayTrongTuans(
		List<TblQLDTTXADMNgayTrongTuan> tblQLDTTXADMNgayTrongTuans) {
	this.tblQLDTTXADMNgayTrongTuans = tblQLDTTXADMNgayTrongTuans;
}

public void setTblQLDTTXAHocViens(List<TblQLDTTXAHocVien> tblQLDTTXAHocViens) {
	this.tblQLDTTXAHocViens = tblQLDTTXAHocViens;
}

public void setTblQLDTTXADMTietHocs(
		List<TblQLDTTXADMTietHoc> tblQLDTTXADMTietHocs) {
	this.tblQLDTTXADMTietHocs = tblQLDTTXADMTietHocs;
}

public void setTblQLDTTXAHocKyTrongNams(
		List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams) {
	this.tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNams;
}

public void setTblQLDTTXALopMonHocs(
		List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs) {
	this.tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocs;
}

public void setView(int view) {
	this.view = view;
} 
 } 
 
